package com.hwadee.JPKC.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hwadee.JPKC.entity.comment;
import com.hwadee.JPKC.entity.user;
import com.hwadee.JPKC.service.CommentService;
import com.hwadee.JPKC.service.CourseInfoService;
import com.hwadee.JPKC.service.courseViewService;

/*
 * 评论控制
 */
@Controller
@RequestMapping("/Conmment")
public class CommentController {
	
	@Autowired
	private CourseInfoService courseInfoService;
	@Autowired
	private courseViewService courseviewservice;
	@Autowired
	private CommentService commentservice;
	
	/*
	 * 用户评论控制
	 */
	@PostMapping("/pinglun")
	public String pinglun(Integer courseId, comment comment, Model model, HttpServletRequest request, HttpSession session) {
		if (session.getAttribute("u") == null) {
			return "accessDenied";
		} else {
			user u = (user) session.getAttribute("u");
			System.out.println(u.toString());
			if (u.getUserRole().equals("专家")) {
				int num = commentservice.insert(comment);
				System.err.println(num);
				String url = "redirect:/Info/courseInfos?courseId=" + courseId;
				return url;
			} else if (u.getUserRole().equals("老师")) {
				/*session.setAttribute("errorMsg", "老师不能评论");
				String url = "redirect:/Info/courseInfos?courseId=" + courseId;*/
				return "accessDenied";

			} else {
				/*session.setAttribute("errorMsg", "学生不能评论");
				String url = "redirect:/Info/courseInfos?courseId=" + courseId;*/
				return "accessDenied";
			}

		}
	}
	
}
